// pages/coupon/lottery.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        awards: [{
            id: "47",
            logo: "https://oss.sitongruisi.com/2c43c9928221e78b0048800c1c57d1f.png",
            probability: "50.00",
            title: "一等奖",
          }, {
            id: "46",
            logo: "https://oss.sitongruisi.com/06cf42dfc7f57d59b0c089223d70a5d.png",
            probability: "50.00",
            title: "二等奖"
          }, {
            id: "48",
            logo: "https://oss.sitongruisi.com/e6d33252d94f3247ab0a5c7b578fb36.png",
            probability: "0.00",
            title: "三等奖"
          }, {
            id: "50",
            logo: "https://shop.duzunds.com/attachment/images/2/2023/01/SQoqk8XDv5qfeF5NQFQKVM9Vf5UyK5.png",
            probability: "0.00",
            title: "谢谢参与"
          }, {
            id: "47",
            logo: "https://oss.sitongruisi.com/2c43c9928221e78b0048800c1c57d1f.png",
            probability: "50.00",
            title: "一等奖"
          }, {
            id: "46",
            logo: "https://oss.sitongruisi.com/06cf42dfc7f57d59b0c089223d70a5d.png",
            probability: "50.00",
            title: "二等奖"
          }, {
            id: "48",
            logo: "https://oss.sitongruisi.com/e6d33252d94f3247ab0a5c7b578fb36.png",
            probability: "0.00",
            title: "三等奖"
          }, {
            id: "50",
            logo: "https://shop.duzunds.com/attachment/images/2/2023/01/SQoqk8XDv5qfeF5NQFQKVM9Vf5UyK5.png",
            probability: "0.00",
            title: "谢谢参与"
          }],
      runDeg: 0,
      cishu: 2,
      awardsList: [],

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.draw()
    },
    draw() {
        var awards = this.data.awards;
        var awardsList = [];
        var turnNum = 1 / awards.length * 360; // 文字旋转 turn 值  
        // 奖项列表  
        for (var i = 0; i < awards.length; i++) {
          awardsList.push({
            turn: (i * turnNum) + 22.5 + 'deg',
            lineTurn: i * turnNum + turnNum / 2 + 'deg',
            award: awards[i].title,
            id: awards[i].id,
            logo: awards[i].logo,
            probability: awards[i].probability
          });
        }
        this.setData({
          awardsList: awardsList
        })
      },
    
        start(){
            wx.request({
          method: "get",
          url: "---------",
          data: {
            user_id: wx.getStorageSync('users').id,
          },
          success: (t) => {
            this.setData({
              cishu: t.data.state,
              msg: t.data.msg,
              lucky_draw: t.data.type
            })
            setTimeout(()=>{
              this.startRotate()
            })
          }
        })
    
        },
    
     startRotate() {
        let userinfo = wx.getStorageSync('userInfo');
        if (!userinfo) return this.setData({
          showModal: true
        }), app.getUserInfo(function (s) {})
    
        let that = this
    
        if (that.data.msg == '点的太快了' && that.data.cishu == '0') {
          wx.showToast({
            title: '点的太快了',
            icon: 'none'
          })
          return
        }
    
        if (that.data.cishu == 1 && !that.data.isChance) {
          wx.showToast({
            title: '分享再得一次抽奖机会',
            icon: 'none'
          })
          return
        }
    
        if (that.data.cishu == 0) {
          wx.showToast({
            title: '抽奖次数不足',
            icon: 'none'
          })
          return
        }
    
        wx.showModal({
          cancelColor: '#666666',
          cancelText: '取消',
          content: '开始免费抽奖',
          success(res) {
            if (res.confirm) {
    
              // 获取上次抽奖记录 lucky_draw:'1' || '2' || '0'
              let prve = that.data.lucky_draw === '0' ? null : that.data.lucky_draw
              // 输出0||1
              let isFirst = () => Math.round(Math.random() + 1)
              // 输出[0, 1] || [4, 5]
              let resultIndex = [
                [0, 1],
                [4, 5]
              ][isFirst() - 1]
              // 抽奖
              let awardsNum = that.data.awardsList;
              // 处理一等奖，二等奖概率
              let awardIndex = prve ? prve == '1' ? resultIndex[1] : resultIndex[0] : resultIndex[isFirst() - 1]
              let runNum = 6; //旋转6周
              let duration = 4000; //时长
              that.data.runDeg = that.data.runDeg || 0;
              that.data.runDeg = that.data.runDeg + (360 - that.data.runDeg % 360) + (360 * runNum - awardIndex * (360 / awardsNum.length) - 22.5);//计算
              that.data.animation.rotate(that.data.runDeg).step(); // 表示一组动画完成
              that.data.animationData = that.data.animation.export(); // 这个方法是导出动画数据（传递给animationData属性）
              that.setData({
                animationData: that.data.animationData,
                deg: that.data.deg,
                awardType: that.data.awardsList[awardIndex].id
              })
    
              setTimeout(()=>{
                that.getDraw(that.data.awardsList[awardIndex].id)
              },4100)
            }
          }
        })
    
      },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})